package com.yellowpage.white.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>Title: StringTools.java</p>
 * <p>Description: 字符串处理工具</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: 上海复旦光华</p>
 * @author tiger xiao
 * @version 1.0
 */
public class StringTools {

	/**
	 * 用字符串替换指定字符串的子字符串
	 * @param line 需要处理的字符串
	 * @param oldString 需要替换的子字符串
	 * @param newString
	 * @return 替换后的字符串
	 */
    public static final String replace(String line, String oldString, String newString) {
        if (line == null) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j = i;
            while ((i = line.indexOf(oldString, i)) > 0) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            return buf.toString();
        }
        return line;
    }

    /**
     * 字符串转换成日期
     * @param sDate 字符串
     * @param sFormat 字符串格式  "yyyy-MM-dd HH:mm:ss"
     * @return 字符串对应的日期信息
     */
    public static Date string2date(String sDate, String sFormat) {
        SimpleDateFormat sf = new SimpleDateFormat(sFormat);
        try {
            Date date = sf.parse(sDate);
            return date;
        } catch (Exception ex) {
            return new Date();
        }
    }

    /**
     *将Date（）转化为String类型
     */
    public static String date2string(Date date) {
        return date2string(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String date2string(Date date, String format) {
        if (date == null) {
            date = new java.util.Date();
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.format(date);
    }

	


	/**
	 * 获取Long型值取指定的模后，转换为字符串值
	 * @param value
	 * @param modValue
	 * @return 去摸后的字符串值
	 */
	public static String modLong(long value,int modValue) {
		return Long.toString(value%1000);
	}

	/**
     * Int 与 String互相转换
     * @param value 需要转换的字符串
     * @param defaultVal 转换出现异常时的默认值
     * @return 转换后的Int
     */
    public static int string2Int(String value, int defaultVal)
    {
        try {
            return Integer.parseInt(value);
        } catch (Exception e)
        {
            return defaultVal;
        }
    }

    /**
     * 字符串转换为Long
     * @param value 需要转换的字符串
     * @param defaultVal 转换出现异常时的默认值
     * @return 转换后的Long
     */
    public static long string2Long(String value, long defaultVal)
    {
        try {
            return Long.parseLong(value);
        } catch (Exception e)
        {
            return defaultVal;
        }
    }

    /**
     * 字符串转换为Double
     * @param value 需要转换的字符串
     * @param defaultVal 转换出现异常时的默认值
     * @return 转换后的Long
     */
    public static double string2Double(String value, double defaultVal)
    {
        try {
            return Double.parseDouble(value);
        } catch (Exception e)
        {
            return defaultVal;
        }
    }
   /**
    * 处理null值
    */
    public static String dealNull(String inputStr){
    	if(inputStr==null){
    		return "";
    	}else{
    		return inputStr;
    	}
    }

    /**
     * 把输入的txt形式的字符串，以html格式输出。
     * @param strTxt
     * @return String
     */
    public static String txt2Html(String text)
    {
    	if (text == null || text.length()<=0)
    		return "&nbsp;";

    	StringBuffer sb = new StringBuffer(text.length() + text.length());
    	char ch = ' ';
    	for (int i=0,n=text.length();i<n;i++)
    	{
    		ch = text.charAt(i);

    		if (ch == ' ')
    			sb.append("&nbsp;");
    		else if (ch == '<')
    			sb.append("&lt;");
    		else if (ch == '>')
    			sb.append("&gt;");
    		else if (ch == '"')
    			sb.append("&quot;");
    		else if (ch == '&')
    			sb.append("&amp;");
    		else if (ch == '\t')
    			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
    		else
    			sb.append(ch);
    	}

    	text = sb.toString().replaceAll("\n\r","<br>");
    	text = text.replaceAll("\n","<br>");

    	return text;
    }
}
